<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables, $level_factor, $sector_max;
	
// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

// Pull query string variables
// Look for POST variables in a check fighters state.
if (isset($_POST['engage']))
	$engage = $_POST['engage'];
elseif (isset($_GET['engage']))
	$engage = $_GET['engage'];

if (isset($_POST['destination']))
	$destination = $_POST['destination'];
elseif (isset($_GET['destination']))
	$destination = $_GET['destination'];
	
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if (isset($destination))
{
	$deg = pi() / 180;
	
	$destination = round(abs($destination));

	$result = $smcFunc['db_query']('', '
		SELECT angle1, angle2, distance
		FROM {raw:db_universe}
		WHERE sector_id = {int:sector_id}',
		array(
			'db_universe' => $dbtables['universe'],
			'sector_id' => $playerinfo['sector']
		)
	);
	$start = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);

	$result = $smcFunc['db_query']('', '
		SELECT angle1, angle2, distance
		FROM {raw:db_universe}
		WHERE sector_id = {int:destination_id}',
		array(
			'db_universe' => $dbtables['universe'],
			'destination_id' => $destination
		)
	);
	$finish = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);

	$sa1 = $start['angle1'] * $deg;
	$sa2 = $start['angle2'] * $deg;
	$fa1 = $finish['angle1'] * $deg;
	$fa2 = $finish['angle2'] * $deg;
	$x = ($start['distance'] * sin($sa1) * cos($sa2)) - ($finish['distance'] * sin($fa1) * cos($fa2));
	$y = ($start['distance'] * sin($sa1) * sin($sa2)) - ($finish['distance'] * sin($fa1) * sin($fa2));
	$z = ($start['distance'] * cos($sa1)) - ($finish['distance'] * cos($fa1));
	$distance = round(sqrt(mypw($x, 2) + mypw($y, 2) + mypw($z, 2)));
	$shipspeed = mypw($level_factor, $playerinfo['engines']);
	$triptime = round($distance / $shipspeed);
		
	if ($triptime == 0 && $destination != $playerinfo['sector'])
		$triptime = 1;

	if ($destination == $playerinfo['sector'])
	{
		$triptime = 0;
		$energyscooped = 0;
	}

	if (($destination < $sector_max && empty($engage)) || ($destination < $sector_max && $triptime > 100 && $engage == 1))
	{
		$free_power = NUM_ENERGY($playerinfo['power']) - $playerinfo['ship_energy'];
		$energyscooped = energy_scooped($playerinfo, $distance, $triptime, $free_power);

		$txt['bnt_rs_dest'] = str_replace("[dest]", $destination, $txt['bnt_rs_dest']);
		$txt['bnt_rs_movetime'] = str_replace("[triptime]", NUMBER($triptime), $txt['bnt_rs_movetime']);
		$txt['bnt_rs_energy'] = str_replace("[energy]", NUMBER($energyscooped), $txt['bnt_rs_energy']);
		
		echo '
					<input id="status" type="hidden" value="error" />
					', $txt['bnt_rs_dest'], '<br />',$txt['bnt_rs_movetime'], '<br />', $txt['bnt_rs_energy'], '<br />';
	
		if ($triptime > $playerinfo['turns'])
			echo '
					', $txt['bnt_rs_noturns'];
		else
		{
			$txt['bnt_rs_engage_link'] = '<a id="engage_2" href="#">' . $txt['bnt_rs_engage_link'] . '</a>';
			$txt['bnt_rs_engage'] = str_replace("[engage]", $txt['bnt_rs_engage_link'], $txt['bnt_rs_engage']);
			
			echo '
					', $txt['bnt_rs_engage'], '<br />
					<input type="hidden" id="dest_sector" value="', $destination, '" />';
		}
	}
	elseif ($destination < $sector_max && $engage > 0)
	{
		$free_power = NUM_ENERGY($playerinfo['power']) - $playerinfo['ship_energy'];
		$energyscooped = energy_scooped($playerinfo, $distance, $triptime, $free_power);
		
		// Check if player has enough turns.
		if ($triptime > $playerinfo['turns'])
		{		
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_ships}
				SET cleared_defences = NULL
				WHERE ship_id = {int:ship_id}',
				array(
					'db_ships' => $dbtables['ships'],
					'ship_id' => $playerinfo['ship_id']
				)
			);
			
			$txt['bnt_rs_movetime'] = str_replace("[triptime]", NUMBER($triptime), $txt['bnt_rs_movetime']);
			
			echo '
					<input id="status" type="hidden" value="error" />
					', $txt['bnt_rs_movetime'], '<br />', $txt['bnt_rs_noturns'];
		}
		// Player has enough turns and there are no further error conditions, engage.
		else
		{
			// These variables are used by check_fighters
		    $ok = 1;
		    $sector = $destination;
		    $calledfrom = "rsmove";
			
		    include("check_fighters.php");
			
		    if ($ok > 0)
		    {
				$update = $smcFunc['db_query']('', '
					UPDATE {raw:db_ships}
					SET last_login = NOW(),
						sector = {int:destination},
						ship_energy = ship_energy + {int:energyscooped},
						turns = turns - {int:triptime},
						turns_used = turns_used + {int:triptime}
					WHERE ship_id = {int:ship_id}',
					array(
						'db_ships' => $dbtables['ships'],
						'destination' => $destination,
						'energyscooped' => $energyscooped,
						'triptime' => $triptime,
						'ship_id' => $playerinfo['ship_id']
					)
				);
				
				$result = $smcFunc['db_query']('', '
					SELECT turns, turns_used, ship_energy
					FROM {raw:db_ships}
					WHERE user_id = {int:user_id}
					LIMIT 1',
					array(
						'db_ships' => $dbtables['ships'],
						'user_id' => $user_info['id'],
					)
				);
				$moveresult = $smcFunc['db_fetch_assoc']($result);
				$smcFunc['db_free_result']($result);
				
				log_move($playerinfo['ship_id'], $destination);

				echo '
							<input id="triptime" type="hidden" value="', $triptime, '" />
							<input id="turns_available" type="hidden" value="', NUMBER($moveresult['turns']), '" />
							<input id="turns_used" type="hidden" value="', NUMBER($moveresult['turns_used']), '" />
							<input id="energy_scooped" type="hidden" value="', $energyscooped, '" />
							<input id="ship_energy" type="hidden" value="', NUMBER($moveresult['ship_energy']), '" />';
				
				include("check_mines.php");
				
				if ($ok == 1)
					echo '
								<input id="status" type="hidden" value="complete" />';		
				else
					echo '
								<input id="status" type="hidden" value="mines" />';
			}
		}
	}
	// Player attempted to move to an invalid sector.
	else
	{
		echo '
							<input id="status" type="hidden" value="error" />
							', $txt['bnt_rs_invalid'];
		
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET cleared_defences = NULL
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $playerinfo['ship_id']
			)
		);
	}
}
?>
